from sklearn.preprocessing import PolynomialFeatures
import numpy as np

X = np.array([[2]])  # 单特征
poly = PolynomialFeatures(degree=3)
X_poly = poly.fit_transform(X)

print("原始特征:", X)  # [[2]]
print("扩展后特征:", X_poly)  # [[1, 2, 4, 8]]
print("特征名称:", poly.get_feature_names_out())  # ['1', 'x0', 'x0^2', 'x0^3']



print("---------------------------------------------------------------")




from sklearn.preprocessing import PolynomialFeatures
import numpy as np

X = np.array([[2, 3]])  # 双特征
poly = PolynomialFeatures(degree=3)
X_poly = poly.fit_transform(X)


print("原始特征:", X)  # [[2, 3]]
print("扩展后特征:", X_poly)  # [[ 1.  2.  3.  4.  6.  9.  8. 12. 18. 27.]]
print("特征名称:", poly.get_feature_names_out())  # ['1' 'x0' 'x1' 'x0^2' 'x0 x1' 'x1^2' 'x0^3' 'x0^2 x1' 'x0 x1^2' 'x1^3']


print("---------------------------------------------------------------")
from sklearn.preprocessing import PolynomialFeatures
import numpy as np

X = np.array([[1, 2, 3]])  # 三特征
poly = PolynomialFeatures(degree=3, interaction_only=False)
X_poly = poly.fit_transform(X)

print("原始特征:", X)  # [[1, 2, 3]]
print("扩展后特征:", X_poly)
#  [[ 1.
#  1.  2.  3.
#  1.  2.  3.  4.  6.  9.
#  1.  2.  3.  4.  6.  9.  8. 12.  18. 27.]]
print("特征名称:", poly.get_feature_names_out())
#['1'
# 'x0' 'x1' 'x2' 'x0^2' 'x0 x1' 'x0 x2' 'x1^2' 'x1 x2' 'x2^2'
# 'x0^3' 'x0^2 x1' 'x0^2 x2' 'x0 x1^2' 'x0 x1 x2' 'x0 x2^2' 'x1^3' 'x1^2 x2' 'x1 x2^2' 'x2^3']